package com.example.TestJDBC;

import java.sql.*;
import java.util.Properties;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2020/11/18 9:44
 * @description:                            TODO   测试 kylin 的  JDBC 驱动
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
public class KylinJDBCTest {


    public static void main(String[] args) throws Exception {
        KylinJDBCTest k = new KylinJDBCTest();
        k.testMyPre();
//        k.testPreparedStatement();

    }


    public void testMyPre() throws Exception {
        String driverClass = "org.apache.kylin.jdbc.Driver";
        String url = "jdbc:kylin://localhost:17070/learn_kylin";
        String username = "ADMIN";
        String password = "KYLIN";
        // 1. 加载 驱动
        Class.forName(driverClass);
        // 2. 获取连接
        Connection connection = DriverManager.getConnection(url, username, password);
        // 3. sql
        String sql = "select * from kylin_account where ACCOUNT_ID=?";
        // 4. 预编译 sql
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setLong(1, 10004316);
        // 5. 执行 sql
        ResultSet rs = ps.executeQuery();
        // 6. 打印
        while (rs.next()) {
            System.out.print(rs.getString(1)+"   ");
            System.out.print(rs.getString(2)+"   ");
            System.out.print(rs.getString(3)+"   ");
            System.out.print(rs.getString(4)+"   ");
            System.out.println("  ");
        }
        // 7. 关闭连接
        rs.close();
        connection.close();
    }

    /**
     * 使用PreparedStatementv查询
     */
    public void testPreparedStatement() throws Exception {
        Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance();
        Properties info = new Properties();
        info.put("user", "ADMIN");
        info.put("password", "KYLIN");
        Connection conn = driver.connect("jdbc:kylin://localhost:17070/learn_kylin", info);
        PreparedStatement state = conn.prepareStatement(" select * from kylin_account where ACCOUNT_ID=? ");
        state.setLong(1, 10004316);
        ResultSet resultSet = state.executeQuery();

        while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
            System.out.println(resultSet.getString(2));
            System.out.println(resultSet.getString(3));
            System.out.println(resultSet.getString(4));
        }
    }


    /**
     * 使用Statement查询
     *
     * @throws Exception
     */
    public void testStatement() throws Exception {


        Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance();

        Properties info = new Properties();
        info.put("user", "ADMIN");
        info.put("password", "KYLIN");
        Connection conn = driver.connect("jdbc:kylin://localhost:7070/kylin_project_name", info);
        Statement state = conn.createStatement();
        ResultSet resultSet = state.executeQuery("select * from test_table");

        while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
            System.out.println(resultSet.getString(2));
            System.out.println(resultSet.getString(3));
        }


    }



    /**
     * 获取查询结果元数据
     */
    public void testSelect() throws Exception {

        Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance();
        Properties info = new Properties();
        info.put("user", "ADMIN");
        info.put("password", "KYLIN");
        Connection conn = driver.connect("jdbc:kylin://localhost:7070/kylin_project_name", info);
        Statement state = conn.createStatement();
        ResultSet resultSet = state.executeQuery("select * from test_table");

        ResultSet tables = conn.getMetaData().getTables(null, null, "dummy", null);
        while (tables.next()) {
            for (int i = 0; i < 10; i++) {
                System.out.println(tables.getString(i + 1));
//                assertEquals("dummy", tables.getString(i + 1));
            }
        }


    }

}
